const app = getApp()
Page({
  gotohome(){
    wx.switchTab({
      url: '../home/home',
    })
  },
  gotodetails(){
     wx.navigateTo({
       url: '../details/details',
     })
  },
  bindMinus: function (e) {
    var num = app.globalData.shopCar[e.currentTarget.dataset.index].orderFormDetails.commodityNum;
    if (num > 1) {
      num--;
    }else if( num==1){
      wx.showModal({
        title: '提示',
        content: '商品数量最小为1哦~',
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return
    }
    var minusStatus = num > 1 ? 'normal' : 'disable';
    app.globalData.shopCar[e.currentTarget.dataset.index].orderFormDetails.commodityNum-=1;
    var sum = 0
    for(var i=0;i<app.globalData.shopCar.length;i++){
        if(app.globalData.shopCar[i].falg){
          sum+=parseFloat(app.globalData.shopCar[i].orderFormDetails.commodityPrice*100*app.globalData.shopCar[i].orderFormDetails.commodityNum)
        }
    }
    this.setData({
      sum: sum/100,
      minusStatus: minusStatus
    })
    this.setData({
      shopCar: app.globalData.shopCar,
      minusStatus: minusStatus
    })
  },
  
  /*点击加号*/
  bindPlus: function (e) {
    var num = app.globalData.shopCar[e.currentTarget.dataset.index].orderFormDetails.commodityNum;
    num++;
    var minusStatus = num > 1 ? 'normal' : 'disable';
    app.globalData.shopCar[e.currentTarget.dataset.index].orderFormDetails.commodityNum+=1;
    var sum = 0
    for(var i=0;i<app.globalData.shopCar.length;i++){
        if(app.globalData.shopCar[i].falg){
          sum+=parseFloat(app.globalData.shopCar[i].orderFormDetails.commodityPrice*100*app.globalData.shopCar[i].orderFormDetails.commodityNum)
        }
    }
    this.setData({
      sum: sum/100,
      minusStatus: minusStatus
    })
    this.setData({
      shopCar: app.globalData.shopCar,
      minusStatus: minusStatus
    })
  },
  /*输入框事件*/
  bindManual: function (e) {
    var num = e.detail.value;
    var minusStatus = num > 1 ? 'normal' : 'disable';
    
  },
  changeAll(){
    this.setData({
      three : !this.data.three
    })
    for(var i=0;i<app.globalData.shopCar.length;i++){
      app.globalData.shopCar[i].falg = this.data.three
    console.log("这是复选"+app.globalData.shopCar[i].falg)
    }
    console.log("这是全选"+this.data.three)
    var sum = 0
    for(var i=0;i<app.globalData.shopCar.length;i++){
        if(app.globalData.shopCar[i].falg){
          sum+=parseFloat(app.globalData.shopCar[i].orderFormDetails.commodityPrice*100*app.globalData.shopCar[i].orderFormDetails.commodityNum)
        }
    }
    this.setData({
      sum: sum/100,
      shopCar:app.globalData.shopCar
    })
  },
  data: {
    nbTitle: '购物车',
    num: 1,
    minusStatus: 'disable',
    shopCar:[],
    sum:0,
    three:false,
  },
  remove(e){
      for(var i=0;i<app.globalData.shopCar.length;i++){
        if(app.globalData.shopCar[i].falg){
          app.globalData.checkedShopCar.push(app.globalData.shopCar[i])
          console.log(app.globalData.checkedShopCar)
        }
      }
      for(var j=0;j<app.globalData.checkedShopCar.length;j++){
        if(app.globalData.shopCar.indexOf(app.globalData.checkedShopCar[j] !== -1)){
          app.globalData.shopCar.splice(app.globalData.shopCar.indexOf(app.globalData.checkedShopCar[j]),1)
        }
      }
    var sum = 0
    for(var i=0;i<app.globalData.shopCar.length;i++){
        if(app.globalData.shopCar[i].falg){
          sum+=app.globalData.shopCar[i].orderFormDetails.commodityPrice*100*app.globalData.shopCar[i].orderFormDetails.commodityNum
        }
    }
    this.setData({
      sum: sum/100,
      shopCar:app.globalData.shopCar
    })
  },
  changeFlag(e){
    var three = true;
    var sum = 0;
    app.globalData.shopCar[e.currentTarget.dataset.index].falg=!app.globalData.shopCar[e.currentTarget.dataset.index].falg
    for(var i=0;i<app.globalData.shopCar.length;i++){
      if(app.globalData.shopCar[i].falg == false){
        three = false
      }else{
        var hehe = (app.globalData.shopCar[i].orderFormDetails.commodityPrice*100*app.globalData.shopCar[i].orderFormDetails.commodityNum)
        sum+=hehe
      }
    }
    if(three){
      this.setData({
        three:three,
        shopCar: app.globalData.shopCar,
        sum:sum/100
      })
    }else{
      this.setData({
        three:three,
        shopCar: app.globalData.shopCar,
        sum:sum/100
      })
    }
  },

  GotoPlaceOrder(){
    var Arr = [];
    for(var i=0;i<app.globalData.shopCar.length;i++){
      if(app.globalData.shopCar[i].falg){
        Arr = [...Arr,app.globalData.shopCar[i]]
      }
    }
    if(Arr == []){
      wx.showModal({
        title: '提示',
        content: '请选择要购买的商品~',
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return
    }
    app.globalData.orderList = Arr
    wx.navigateTo({
      url: '../PlaceOrder/PlaceOrder?Sum='+this.data.sum,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopCar:app.globalData.shopCar
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      shopCar:app.globalData.shopCar
    })
    console.log(this.data.shopCar)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    app.globalData.shopCar=this.data.shopCar
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})